﻿@model WebApp.Models.S_user

@{
    Layout = "~/Views/Shared/_DialogLayout.cshtml";
}
@using (Html.BeginForm("", "", FormMethod.Post, new { @class = "form-horizontal form-inline", id = "inputform" }))
{
    <div class="dialog-content">
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <input type="hidden" id="Id" value="@Model.Id" />
        <input type="hidden" value="@(Model.Updatetime.HasValue ? Model.Updatetime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") : "")" class="form-control datafield" data-fieldname="Updatetime" />

        <table cellpadding="3" cellspacing="0" border="0" class="inputform">
            <tr>
                <td class="td-label">用户名</td>
                <td class="td-control">
                    @Html.TextBox("Username", @Model.Username, new { @class = "form-control datafield", @readonly = "true", @data_fieldname = "Username" })
                </td>
            </tr>
            <tr>
                <td class="td-label">新密码</td>
                <td class="td-control">
                    @Html.Password("Password", "", new { @class = "form-control datafield", @data_fieldname = "Password" })
                </td>
            </tr>
            <tr>
                <td class="td-label">再输一次</td>
                <td class="td-control">
                    @Html.Password("ConfrimPassword", "", new { @class = "form-control datafield", @data_fieldname = "Password" })
                </td>
            </tr>
            <tr style="display:none;">
                <td class="td-label"></td>
                <td class="td-control">
                    @Html.CheckBox("Ischangepwd", false, new { @class = "form-control datafield", @data_fieldname = "Ischangepwd" }) <span>要求用户下次登录时修改密码</span>
                </td>
            </tr>
        </table>
    </div>
    <div class="dialog-footer">
        <button id="btncancel" class="btn btn-small btn-default">取消</button>
        <button id="btnsave" class="btn btn-small btn-primary">确认</button>
    </div>
}
@section scripts{
    <script type="text/javascript">

       //初始化表单验证
       function initValidate() {
           //表单验证
           $('#inputform').validate({
               rules: {
                   Password: {
                       required: true,
                       //remote: {
                       //    url: baseurl + 'Manage/User/PwdLengthChk',
                       //    type: 'post',
                       //    data: {
                       //        password: $("#Password").val()
                       //    },
                       //    dataFilter: function (data, type) {
                       //        if (data == "yes")
                       //            return true;
                       //        else
                       //            return false;
                       //    }
                       //}
                   },
                   ConfrimPassword:
                    {
                        equalTo: "#Password"
                    }
               },
               errorPlacement: function (error, element) {
                   error.appendTo(element.parent());
               },
               messages: {
                   Password: {
                       remote: '您的密码必须是6~16位的字符，且必须是数字和字母的组合。可以使用特殊符号，字母区分大小写！'
                   },
                   ConfrimPassword: {
                       equalTo: '两次密码输入不一致'
                   }
               }
           });
       }
       $(document).ready(function () {
           initValidate();
           //取消
           $("#btncancel").click(function (evt) { evt.preventDefault(); parent.closeDialog(); return false; });

           $("#btnsave").click(function (evt) {
               evt.preventDefault();
               if (!$("#inputform").valid()) {
                   return;
               }

               var jsondata = {
                   User: {
                       Id: $("#Id").val(),
                       Values: {}
                   }
               };
               jsondata.User.Values["Password"] = $("[data-fieldname='Password']").val();
               jsondata.User.Values["Ischangepwd"] = $("[data-filedname='Ischangepwd']").prop("checked") ? "1" : "0";
               $.ajax({
                   type: "post",
                   url: baseurl + "Manage/User/ChangePwd",
                   data: {
                       json: JSON.stringify(jsondata)
                   },
                   success: function (e) {
                       $("#btnsave").attr("disabled", false);
                       if (e.Ret == "0") {
                           parent.closeDialog(true);
                       } else {
                           alert(e.Msg);
                       }
                   }
               });

           });
       });

    </script>
}

